lazy-regex
With lazy-regex macros, regular expressions
- are checked at compile time, with clear error messages
- are wrapped in
once_cell
lazy static initializers so that they're compiled only once - can hold flags as suffix:
let case_insensitive_regex = regex!("ab*"i);
- are defined in a less verbose way
The regex!
macro returns references to normal instances of regex::Regex
or regex::bytes::Regex
so all the usual features are available.
Other macros are specialized for testing a match, replacing with concise closures, or capturing groups as substrings in some common situations:
regex_is_match!
regex_find!
regex_captures!
regex_replace!
regex_replace_all!
regex_switch!
They support the B
flag for the regex::bytes::Regex
variant.
All macros exist with a bytes_
prefix for building bytes::Regex
, so you also have bytes_regex!
, bytes_regex_is_match!
, bytes_regex_find!
, bytes_regex_captures!
, bytes_regex_replace!
, bytes_regex_replace_all!
, and bytes_regex_switch!
.
Some structs of the regex crate are reexported to ease dependency managment. The regex crate itself is also reexported, to avoid the need to synchronize the versions/flavor (see Features below)
Build Regexes
use regex;
// build a simple regex
let r = regex!;
assert_eq!;
// build a regex with flag(s)
let r = regex!;
assert_eq!;
// you can use a raw literal
let r = regex!;
assert_eq!;
// or a raw literal with flag(s)
let r = regex!;
assert_eq!;
// build a regex that operates on &[u8]
let r = regex!;
assert_eq!;
// there's no problem using the multiline definition syntax
let r = regex!;
assert_eq!;
// (look at the regex_captures! macro to easily extract the groups)
// this line doesn't compile because the regex is invalid:
let r = regex!("(unclosed");
Supported regex flags: i
, m
, s
, x
, U
.
See regex::RegexBuilder.
Test a match
use regex_is_match;
let b = regex_is_match!;
assert_eq!;
Extract a value
use regex_find;
let f_word = regex_find!;
assert_eq!;
let f_word = regex_find!;
assert_eq!;
Capture
use regex_captures;
let = regex_captures!.unwrap;
assert_eq!;
let = regex_captures!.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
There's no limit to the size of the tuple. It's checked at compile time to ensure you have the right number of capturing groups.
You receive ""
for optional groups with no value.
Replace with captured groups
The regex_replace!
and regex_replace_all!
macros bring once compilation and compilation time checks to the replace
and replace_all
functions.
Replace with a closure
use regex_replace_all;
let text = "Foo8 fuu3";
let text = regex_replace_all!;
assert_eq!;
The number of arguments given to the closure is checked at compilation time to match the number of groups in the regular expression.
If it doesn't match you get, at compilation time, a clear error message.
Replace with another kind of Replacer
use regex_replace_all;
let text = "UwU";
let output = regex_replace_all!;
assert_eq!;
Switch over regexes
Execute the expression bound to the first matching regex, with named captured groups declared as varibles:
use regex_switch;
Shared lazy static
When a regular expression is used in several functions, you sometimes don't want to repeat it but have a shared static instance.
The regex!
macro, while being backed by a lazy static regex, returns a reference.
If you want to have a shared lazy static regex, use the lazy_regex!
macro:
use *;
pub static GLOBAL_REX: = lazy_regex!;
Like for the other macros, the regex is static, checked at compile time, and lazily built at first use.
Features and reexport
With default features, lazy-regex
use the regex
crate with its default features, tailored for performances and complete Unicode support.
You may enable a different set of regex features by directly enabling them when importing lazy-regex
.
It's also possible to use the regex-lite crate instead of the regex crate by declaring the lite
feature:
= { = "3.0", = false, = ["lite"] }
The lite
flavor comes with slightly lower performances and a reduced Unicode support (see crate documentation) but also a much smaller binary size.
If you need to refer to the regex crate in your code, prefer to use the reexport (i.e. use lazy_regex::regex;
) so that you don't have a version or flavor conflict. When the lite
feature is enabled, lazy_regex::regex
refers to regex_lite
so you don't have to change your code when switching regex engine.